/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package web.usuarios;

import web.usuarios.*;
import Persistencia.*;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import controladores.*;
import dominio.*;
import dominio.SesionDomain;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ControladorSimpleModSesion extends SimpleFormController{

    ControladorSesion controlador;
    ControladorPaciente controladorPac;
    SesionDomain obj;
    String idFisio="";
    @Override
    public ModelAndView onSubmit(Object command) throws ServletException {
        controlador = new ControladorSesion();
        obj=(SesionDomain) command;
        controlador.setSesionD(obj);
        controlador.setSesion(0);        
        controlador.modificarSesion();
        Map<String, Object> myModel = new HashMap<String, Object>();
        myModel.put("id_fisio", idFisio);
        myModel.put("sesiones", controlador.encontrarMisSesiones(idFisio));
        if(obj!=null){
            return new ModelAndView("administrarSesion", "model", myModel);
        }else{
            return new ModelAndView("administrarSesion", "model", myModel);
        }
    }

    protected Object formBackingObject(HttpServletRequest request) throws ServletException {
        System.out.println("juajua log D backing object");
        controlador = new ControladorSesion();
        idFisio=request.getParameter("id_fisio");
        Sesion log = controlador.encontrarSesion(request.getParameter("id_sesion"));
        SesionDomain logD=controlador.convertirDomain(log);
        System.out.println(logD+"log D backing object");
        return logD;
    }

    public void setControladorSesion(ControladorSesion productManager) {
        this.controlador = productManager;
    }

    public ControladorSesion getControladorSesion() {
        return controlador;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected Map referenceData(HttpServletRequest request) throws Exception {
        System.out.println("REFERENCE SESION");
        Map referenceData = new HashMap();
        controlador = new ControladorSesion();
        controladorPac = new ControladorPaciente();
        idFisio=request.getParameter("id_fisio");
        System.out.println(idFisio+" ID FISIO");
        Sesion log = controlador.encontrarSesion(request.getParameter("id_sesion"));
        SesionDomain logD=controlador.convertirDomain(log);
        referenceData.put("modSesion", logD);        
        referenceData.put("listPaciente", controladorPac.encontrarMisPacientes(idFisio));        
        referenceData.put("pacienteEscogido", ""+log.getPaciente().getCedula()+" - "+log.getPaciente().getNombre()+" "+log.getPaciente().getApellido());        
        return referenceData;
    }
}
